package com.application.controller;


import com.application.domain.Question;
import com.application.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/question")
@Api(tags = "用户反馈问题")
public class QuestionController {

    @Resource
    QuestionService questionService;

    @GetMapping("/getById")
    @ApiOperation(value = "查看反馈信息")
    public Result getById(@RequestParam String id){
        Question question = questionService.selectById(id);

        String mes = question !=null ? "查看成功":"没有找到对应的反馈";
        int code = question !=null ? Code.SELECT_OK:Code.SELECT_ERR;

        return  new Result(code,question,mes);
    }

    @PostMapping
    @ApiOperation(value = "用户反馈要求")
    public Result Insert(@RequestBody Question question,@RequestHeader(required = true) String openid){
        question.setOpenid(openid);
        int save = questionService.save(question);

        String mes = save !=0 ? "反馈成功，感谢你的建议":"反馈失败，请联系我们";
        int code = save !=0 ? Code.SAVE_OK:Code.SELECT_ERR;

        return  new Result(code,save,mes);
    }


    @PutMapping
    @ApiOperation(value = "进行修改用户反馈")
    public Result update(@RequestBody Question question,@RequestHeader(required = true) String openid,@RequestHeader(required = true) String id){
        Question question1 = questionService.selectById(id);
        if(openid.equals(question1.getOpenid())){
            int update = questionService.update(question, id);

            String mes = update !=0 ? "更新成功":"更新失败，请重新尝试";
            int code = update !=0? Code.UPDATE_OK:Code.UPDATE_ERR;

            return new Result(code,update,mes);
        }
        return new Result(Code.UPDATE_ERR,null,"抱歉，无法修改");
    }

    @DeleteMapping
    @ApiOperation(value = "用来删除用户反馈的信息")
    public Result deleteById(@RequestParam String id,@RequestParam String openid){
        Question question = questionService.selectById(id);
        if(openid.equals(question.getOpenid())){
            int delete = questionService.delete(id);

            String mes = delete !=0 ? "删除成功":"删除失败，请重新尝试";
            int code = delete !=0 ? Code.DELETE_OK:Code.DELETE_ERR;

            return new Result(code,delete,mes);
        }

        return new Result(Code.DELETE_ERR,null,"无法进行删除");
    }

}
